﻿#region 信息

// Copyright©2014 Rex Rao all rights reserved.
// File name : SoMcCoffee\Sohobloo.SoPermission.Web\ModelEntityConvertor.cs
// Author : Sohobloo
// Create time : 2014/02/16 14:25
// Update time : 2014/02/16 14:28

#endregion

#region 引用

using Sohobloo.SoCommon;
using Sohobloo.SoCommon.CommonExtension;
using Sohobloo.SoPermission.Entity;
using Sohobloo.SoPermission.Web.Models;

#endregion

namespace Sohobloo.SoPermission.Web
{
    public static class ModelEntityConvertor
    {
        public static GroupModel ToModel(this Group group)
        {
            ArgumentHelper.Instance.CheckNull(group);

            var groupModel = new GroupModel
            {
                Id = group.Id,
                Name = group.Name,
                Description = group.Description,
                Enabled = group.Enabled,
            };

            return groupModel;
        }

        public static Group ToEntity(this GroupModel groupModel)
        {
            ArgumentHelper.Instance
                          .CheckNull(groupModel)
                          .CheckEmpty(groupModel.Name);

            var group = new Group
            {
                Name = groupModel.Name.Trim(),
                Description = groupModel.Description.NullToEmpty().Trim(),
                Enabled = groupModel.Enabled
            };

            return group;
        }

        public static Group ToEntity(this GroupModel groupModel, Group group)
        {
            ArgumentHelper.Instance
                          .CheckNull(groupModel)
                          .CheckEmpty(groupModel.Name);

            group.Name = groupModel.Name.Trim();
            group.Description = groupModel.Description.NullToEmpty().Trim();
            group.Enabled = groupModel.Enabled;

            return group;
        }
    }
}